<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="原创,密码学,ElGamal,Miracl库," />










<meta name="description" content="前言这是西电网信院信息安全数学基础的实验题目，要求使用miracl库实现ElGamal加解密。实现代码可在此链接中下载，提取码为ptwk。">
<meta property="og:type" content="article">
<meta property="og:title" content="miracl库实现ElGamal加解密">
<meta property="og:url" content="http://liuyun000.gitee.io/2019/11/22/%E5%AF%86%E7%A0%81%E5%AD%A6/miracl%E5%BA%93%E5%AE%9E%E7%8E%B0ElGamal%E5%8A%A0%E8%A7%A3%E5%AF%86/index.html">
<meta property="og:site_name" content="liuyun的博客">
<meta property="og:description" content="前言这是西电网信院信息安全数学基础的实验题目，要求使用miracl库实现ElGamal加解密。实现代码可在此链接中下载，提取码为ptwk。">
<meta property="og:image" content="http://liuyun000.gitee.io/images/%E5%AE%9E%E7%8E%B0ElGamal_%E9%80%9A%E7%9F%A5.jpg">
<meta property="article:published_time" content="2019-11-22T04:53:55.000Z">
<meta property="article:modified_time" content="2020-01-10T05:18:08.162Z">
<meta property="article:author" content="liuyun">
<meta property="article:tag" content="原创">
<meta property="article:tag" content="密码学">
<meta property="article:tag" content="ElGamal">
<meta property="article:tag" content="Miracl库">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://liuyun000.gitee.io/images/%E5%AE%9E%E7%8E%B0ElGamal_%E9%80%9A%E7%9F%A5.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://liuyun000.gitee.io/2019/11/22/密码学/miracl库实现ElGamal加解密/"/>





  <title>miracl库实现ElGamal加解密 | liuyun的博客</title>
  








<meta name="generator" content="Hexo 4.2.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">liuyun的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://liuyun000.gitee.io/2019/11/22/%E5%AF%86%E7%A0%81%E5%AD%A6/miracl%E5%BA%93%E5%AE%9E%E7%8E%B0ElGamal%E5%8A%A0%E8%A7%A3%E5%AF%86/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="liuyun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="liuyun的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">miracl库实现ElGamal加解密</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-11-22T12:53:55+08:00">
                2019-11-22
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%A4%A7%E5%AD%A6%E4%BD%9C%E4%B8%9A/" itemprop="url" rel="index">
                    <span itemprop="name">大学作业</span>
                  </a>
                </span>

                
                
                  ， 
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%A4%A7%E5%AD%A6%E4%BD%9C%E4%B8%9A/%E5%AF%86%E7%A0%81%E5%AD%A6/" itemprop="url" rel="index">
                    <span itemprop="name">密码学</span>
                  </a>
                </span>

                
                
                  ， 
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%A4%A7%E5%AD%A6%E4%BD%9C%E4%B8%9A/%E5%AF%86%E7%A0%81%E5%AD%A6/%E5%8E%9F%E5%88%9B/" itemprop="url" rel="index">
                    <span itemprop="name">原创</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h3><p>这是西电网信院信息安全数学基础的实验题目，要求使用miracl库实现ElGamal加解密。实现代码可在<a href="https://pan.baidu.com/s/1kdi4nBP0RffdbK8SX-daUw" target="_blank" rel="noopener">此链接</a>中下载，提取码为ptwk。</p>
<a id="more"></a>



<h3 id="要求实现"><a href="#要求实现" class="headerlink" title="要求实现"></a>要求实现</h3><h4 id="ElGamal的密钥生成过程"><a href="#ElGamal的密钥生成过程" class="headerlink" title="ElGamal的密钥生成过程"></a>ElGamal的密钥生成过程</h4><ol>
<li><p>生成随机大素数p，求得p的本原根g（本原根也就是模p乘法域的生成元）。</p>
</li>
<li><p>随机选择私钥x，1 &lt; x &lt; p-2</p>
</li>
<li><p>计算 y = g<sup>x</sup> mod p</p>
<p>计算得到的公钥为(p, g, y)，对外公布。私钥是x，自己私藏。</p>
</li>
</ol>
<h4 id="ElGamal的加密过程"><a href="#ElGamal的加密过程" class="headerlink" title="ElGamal的加密过程"></a>ElGamal的加密过程</h4><p>假设B要与A进行通信，B所要加密的明文为m，使用ElGamal密码算法，B方执行的加密过程如下：</p>
<ol>
<li><p>从A放获得加密所需的公钥(p, g, y)。</p>
</li>
<li><p>选择一随机数k，(k, p-1) = 1，1 &lt;= k &lt;= p-2.</p>
</li>
<li><p>计算  </p>
<p>y<sub>1</sub> = g<sup>k</sup> mod p （随机数k被加密）</p>
<p>再用公钥y，计算：</p>
<p>y<sub>2</sub> = my<sup>k</sup> mod p (明文被随机数k和公钥y加密)</p>
<p>密文c = (y<sub>1</sub>, y<sub>2</sub>)。</p>
</li>
</ol>
<h4 id="ElGamal解密过程"><a href="#ElGamal解密过程" class="headerlink" title="ElGamal解密过程"></a>ElGamal解密过程</h4><p>当A接收到密文c = (y<sub>1</sub>, y<sub>2</sub>)之后，解密过程如下：</p>
<p>使用自己的私钥x计算</p>
<p>m = (y<sub>1</sub><sup>-x</sup>)y<sub>2</sub> mod p</p>
<p>获得明文m。</p>
<h3 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h3><h4 id="获取本原根原理"><a href="#获取本原根原理" class="headerlink" title="获取本原根原理"></a>获取本原根原理</h4><p>其实刚开始看到的时候以为实现起来没有什么困难，但当自己开始动手写的时候发现一个问题：本原根g如何求？</p>
<p>我在网上找了很长时间，没有找到本原根的生成方法，直到找到在<a href="http://www.mathmagic.cn/bbs/simple/?t9356.html" target="_blank" rel="noopener">这个帖子里</a>有人问了同样的问题，@<strong>chinazhaok</strong>这位网友回答了一种方法，并说明在《应用密码学手册》中有相关的算法。</p>
<p>下面是《应用密码学手册》的算法：</p>
<p><strong>算法4.80 找循环群的生成元</strong></p>
<p>​    输入：阶为n的循环群G，n的素因子分解n = p<sup>1</sup>, p<sup>2</sup>, …,  p<sup>k</sup>. </p>
<p>​    输出：C的一个生成元a。</p>
<ol>
<li><p>随机选择一个G中的元素a</p>
</li>
<li><p>对 i 从1到k执行如下操作：</p>
<p>2.1 计算 b &lt;- a<sup>p<sup>i</sup></sup></p>
<p>2.2 若 b = 1，则转到步骤1.</p>
</li>
<li><p>返回 a。</p>
</li>
</ol>
<p>但是问题又来了，大数的素因子分解可本来就是一个难题呀。正当我陷入困境时，课代表的一则通知简直是及时雨点醒了我：</p>
<p><img src="/images/%E5%AE%9E%E7%8E%B0ElGamal_%E9%80%9A%E7%9F%A5.jpg" alt="课代表的通知"></p>
<p>要求p = 2*q+1，而且q也是素数！这样的话φ(p) = p-1 = 2*q，而2*q分解不就是2和q吗。所以这个问题也就迎刃而解了。</p>
<p>实现过程中其他也没什么了，看下面的代码吧，做了尽可能的注释。</p>
<p>-----------------------------------2020-01-10更新--------------------------------------</p>
<h4 id="获取强随机数方法"><a href="#获取强随机数方法" class="headerlink" title="获取强随机数方法"></a>获取强随机数方法</h4><p>用纯随机的方法获取强随机数效果并不好，时间太长，老师验收时说可以在一个随机素数周围寻找强随机数，另一种思想是使用miracl库中的nxsafeprime接口，这里选用的老师说的方法。</p>
<p>-------------------------------------------------------------------------</p>
<h3 id="代码实现"><a href="#代码实现" class="headerlink" title="代码实现"></a>代码实现</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// exp4.c</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;miracl.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;mirdef.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;time.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">enc</span><span class="params">(big m, big p, big g, big y, big cs[<span class="number">2</span>])</span></span>;</span><br><span class="line"><span class="function">big <span class="title">dec</span><span class="params">(big cs[<span class="number">2</span>], big p, big x)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">( <span class="keyword">int</span> argc, <span class="keyword">char</span>* argv[] )</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	miracl *mip = mirsys(<span class="number">1000</span>, <span class="number">10</span>);</span><br><span class="line">	<span class="keyword">char</span> buf[<span class="number">1001</span>] = &#123; <span class="number">0</span> &#125;;</span><br><span class="line">	<span class="keyword">char</span> msg[<span class="number">151</span>] = &#123; <span class="number">0</span> &#125;;</span><br><span class="line">	FILE* fp = <span class="literal">NULL</span>;</span><br><span class="line">	big two, one, p, q, g, x, y, m, c, tmp, psubtwo;</span><br><span class="line">	big cs[<span class="number">2</span>], ret;</span><br><span class="line"></span><br><span class="line">	mip-&gt;IOBASE = <span class="number">10</span>;</span><br><span class="line">	<span class="comment">//读取文件数据</span></span><br><span class="line">	<span class="keyword">if</span> (argc != <span class="number">2</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">fprintf</span>(<span class="built_in">stderr</span>, <span class="string">"usage: %s &lt;message-file-path&gt;\n"</span>, argv[<span class="number">0</span>]);</span><br><span class="line">		<span class="keyword">goto</span> <span class="built_in">exit</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> ((fp = fopen(argv[<span class="number">1</span>], <span class="string">"r"</span>)) == <span class="literal">NULL</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">fprintf</span>(<span class="built_in">stderr</span>, <span class="string">"file open err: can't open file %s !\n"</span>, argv[<span class="number">1</span>]);</span><br><span class="line">		<span class="keyword">goto</span> <span class="built_in">exit</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (fread(msg, <span class="keyword">sizeof</span>(<span class="keyword">char</span>), <span class="number">150</span>, fp) &lt;= <span class="number">0</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">fprintf</span>(<span class="built_in">stderr</span>, <span class="string">"file read err: can't read file %s !\n"</span>, argv[<span class="number">1</span>]);</span><br><span class="line">		fclose(fp);</span><br><span class="line">		<span class="keyword">goto</span> <span class="built_in">exit</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	fclose(fp);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//初始化大数</span></span><br><span class="line">	one = mirvar(<span class="number">1</span>);</span><br><span class="line">	two = mirvar(<span class="number">2</span>);</span><br><span class="line">	p = mirvar(<span class="number">0</span>);</span><br><span class="line">	q = mirvar(<span class="number">0</span>);</span><br><span class="line">	g = mirvar(<span class="number">2</span>);</span><br><span class="line">	x = mirvar(<span class="number">0</span>);</span><br><span class="line">	y = mirvar(<span class="number">0</span>);</span><br><span class="line">	m = mirvar(<span class="number">0</span>);</span><br><span class="line">	c = mirvar(<span class="number">0</span>);</span><br><span class="line">	tmp = mirvar(<span class="number">0</span>);</span><br><span class="line">	psubtwo = mirvar(<span class="number">0</span>);</span><br><span class="line">	irand(time(<span class="literal">NULL</span>));</span><br><span class="line"></span><br><span class="line">	<span class="comment">//</span></span><br><span class="line">	cinstr(m, msg);</span><br><span class="line">	</span><br><span class="line"></span><br><span class="line">	<span class="comment">//选取随机素数q和p</span></span><br><span class="line">	<span class="keyword">while</span> (<span class="number">1</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		bigbits(<span class="number">499</span>, q);</span><br><span class="line">		<span class="keyword">if</span> (isprime(q))</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">while</span> (<span class="number">1</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		add(q, two, q);</span><br><span class="line">		<span class="keyword">if</span> (!isprime(q))</span><br><span class="line">		&#123;	</span><br><span class="line">			<span class="keyword">continue</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		add(q, q, p);</span><br><span class="line">		add(p, one, p);</span><br><span class="line">		<span class="keyword">if</span> (isprime(p))</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/*do &#123;</span></span><br><span class="line"><span class="comment">		bigbits(500, tmp);</span></span><br><span class="line"><span class="comment">	&#125; while (!nxsafeprime(0, 0, tmp, p));</span></span><br><span class="line"><span class="comment">	decr(p, 1, q);</span></span><br><span class="line"><span class="comment">	sftbit(q, 1, q);*/</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">//获取生成元g</span></span><br><span class="line">	<span class="keyword">do</span> &#123;</span><br><span class="line">		powmod(g, two, p, tmp);</span><br><span class="line">		<span class="keyword">if</span> (mr_compare(tmp, one) &gt; <span class="number">0</span>)</span><br><span class="line">		&#123;</span><br><span class="line">			powmod(g, q, p, tmp);</span><br><span class="line">			<span class="keyword">if</span> (mr_compare(tmp, one) &gt; <span class="number">0</span>) &#123;</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		add(g, one, g);</span><br><span class="line"></span><br><span class="line">	&#125; <span class="keyword">while</span> (mr_compare(g, p) &lt; <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//随机取x</span></span><br><span class="line">	decr(p, <span class="number">2</span>, psubtwo);</span><br><span class="line">	<span class="keyword">do</span> &#123;</span><br><span class="line">		bigrand(psubtwo, x);</span><br><span class="line">	&#125; <span class="keyword">while</span> (mr_compare(x, one) &lt;= <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//计算y</span></span><br><span class="line">	powmod(g, x, p, y);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//输出公钥</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"公钥：\n"</span>);</span><br><span class="line">	<span class="built_in">memset</span>(buf, <span class="number">0</span>, <span class="number">501</span>);</span><br><span class="line">	cotstr(p, buf);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"p = %s\n"</span>, buf);</span><br><span class="line">	<span class="built_in">memset</span>(buf, <span class="number">0</span>, <span class="number">501</span>);</span><br><span class="line">	cotstr(g, buf);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"g = %s\n"</span>, buf);</span><br><span class="line">	<span class="built_in">memset</span>(buf, <span class="number">0</span>, <span class="number">501</span>);</span><br><span class="line">	cotstr(y, buf);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"y = %s\n"</span>, buf);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="comment">//输出私钥</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"私钥：\n"</span>);</span><br><span class="line">	<span class="built_in">memset</span>(buf, <span class="number">0</span>, <span class="number">501</span>);</span><br><span class="line">	cotstr(x, buf);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"x = %s\n"</span>, buf);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//加密</span></span><br><span class="line">	enc(m, p, g, y, cs);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//输出y1和y2</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"密文：\n"</span>);</span><br><span class="line">	<span class="built_in">memset</span>(buf, <span class="number">0</span>, <span class="number">501</span>);</span><br><span class="line">	cotstr(cs[<span class="number">0</span>], buf);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"y1 = %s\n"</span>, buf);</span><br><span class="line">	<span class="built_in">memset</span>(buf, <span class="number">0</span>, <span class="number">501</span>);</span><br><span class="line">	cotstr(cs[<span class="number">1</span>], buf);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"y2 = %s\n"</span>, buf);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//解密</span></span><br><span class="line">	ret = dec(cs, p, x);</span><br><span class="line"></span><br><span class="line">	<span class="comment">//输出还原的消息</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"明文：\n"</span>);</span><br><span class="line">	<span class="built_in">memset</span>(buf, <span class="number">0</span>, <span class="number">501</span>);</span><br><span class="line">	cotstr(ret, buf);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"message = %s\n"</span>, buf);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">"\n"</span>);</span><br><span class="line"></span><br><span class="line">	system(<span class="string">"pause"</span>);</span><br><span class="line"><span class="built_in">exit</span>:</span><br><span class="line">	mirexit();</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">enc</span><span class="params">(big m, big p, big g, big y, big cs[<span class="number">2</span>])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	big one, psubone, k, tmp;</span><br><span class="line">	one = mirvar(<span class="number">1</span>);</span><br><span class="line">	psubone = mirvar(<span class="number">0</span>);</span><br><span class="line">	k = mirvar(<span class="number">0</span>);</span><br><span class="line">	tmp = mirvar(<span class="number">0</span>);</span><br><span class="line">	cs[<span class="number">0</span>] = mirvar(<span class="number">0</span>);</span><br><span class="line">	cs[<span class="number">1</span>] = mirvar(<span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">	decr(p, <span class="number">1</span>, psubone);</span><br><span class="line">	<span class="keyword">while</span> (<span class="number">1</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		bigrand(psubone, k);</span><br><span class="line">		<span class="keyword">if</span> (mr_compare(k, one) &gt;= <span class="number">0</span>)</span><br><span class="line">		&#123;</span><br><span class="line">			egcd(k, y, tmp);</span><br><span class="line">			<span class="keyword">if</span> (mr_compare(tmp, one) == <span class="number">0</span>)</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	powmod(g, k, p, cs[<span class="number">0</span>]);</span><br><span class="line">	powmod(y, k, p, cs[<span class="number">1</span>]);</span><br><span class="line">	multiply(cs[<span class="number">1</span>], m, cs[<span class="number">1</span>]);</span><br><span class="line">	powmod(cs[<span class="number">1</span>], one, p, cs[<span class="number">1</span>]);</span><br><span class="line"></span><br><span class="line">	mirkill(one);</span><br><span class="line">	mirkill(psubone);</span><br><span class="line">	mirkill(k);</span><br><span class="line">	<span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function">big <span class="title">dec</span><span class="params">(big cs[<span class="number">2</span>], big p, big x)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	big tmp, one, ret;</span><br><span class="line"></span><br><span class="line">	tmp = mirvar(<span class="number">0</span>);</span><br><span class="line">	ret = mirvar(<span class="number">0</span>);</span><br><span class="line">	one = mirvar(<span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">	powmod(cs[<span class="number">0</span>], x, p, tmp);</span><br><span class="line">	xgcd(tmp, p, tmp, tmp, tmp);</span><br><span class="line">	fft_mult(cs[<span class="number">1</span>], tmp, tmp);</span><br><span class="line">	powmod(tmp, one, p, ret);</span><br><span class="line"></span><br><span class="line">	mirkill(tmp);</span><br><span class="line">	mirkill(one);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/%E5%8E%9F%E5%88%9B/" rel="tag"># 原创</a>
          
            <a href="/tags/%E5%AF%86%E7%A0%81%E5%AD%A6/" rel="tag"># 密码学</a>
          
            <a href="/tags/ElGamal/" rel="tag"># ElGamal</a>
          
            <a href="/tags/Miracl%E5%BA%93/" rel="tag"># Miracl库</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/11/05/unix/Unix%E7%8E%AF%E5%A2%83%E9%AB%98%E7%BA%A7%E7%BC%96%E7%A8%8B%EF%BC%9A%E8%BF%9B%E7%A8%8B%E7%8E%AF%E5%A2%83/" rel="next" title="Unix环境高级编程：进程环境">
                <i class="fa fa-chevron-left"></i> Unix环境高级编程：进程环境
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2020/01/10/C++/C++%E6%A8%A1%E6%9D%BF%E4%BB%A3%E7%A0%81%E7%BB%84%E7%BB%87%E6%96%B9%E5%BC%8F/" rel="prev" title="C++模板代码组织方式">
                C++模板代码组织方式 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">liuyun</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/%20%7C%7C%20archive">
              
                  <span class="site-state-item-count">16</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">9</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">21</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#要求实现"><span class="nav-number">2.</span> <span class="nav-text">要求实现</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#ElGamal的密钥生成过程"><span class="nav-number">2.1.</span> <span class="nav-text">ElGamal的密钥生成过程</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#ElGamal的加密过程"><span class="nav-number">2.2.</span> <span class="nav-text">ElGamal的加密过程</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#ElGamal解密过程"><span class="nav-number">2.3.</span> <span class="nav-text">ElGamal解密过程</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#实现"><span class="nav-number">3.</span> <span class="nav-text">实现</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#获取本原根原理"><span class="nav-number">3.1.</span> <span class="nav-text">获取本原根原理</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#获取强随机数方法"><span class="nav-number">3.2.</span> <span class="nav-text">获取强随机数方法</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#代码实现"><span class="nav-number">4.</span> <span class="nav-text">代码实现</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">liuyun</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/miku.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
